<?php

namespace app\api\controller;

use app\api\model\Coupon as CouponModel;

/**
 * 优惠券中心
 * Class Coupon
 * @package app\api\controller
 */
class Coupon extends Controller
{
	
	/* @var UserCouponModel $model */
    private $model;

    /* @var \app\api\model\User $model */
    private $user;

    /**
     * 构造方法
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function initialize()
    {
        //parent::initialize();
        //$this->model = new UserCouponModel;
        $this->user = $this->getShangUser();   // 商户信息
        $this->imgadess = $this->pcUrl();
    }

    /**
     * 优惠券列表
     */
    public function youHuiLists()
    {
        $info = $this->request->post();
        if(!empty($info['type'])){
            if($info['type'] == '10'){
                $couwhe['apply_range'] = '10';
            }else{
                $couwhe['apply_range'] = '20';
            }
        }
        $couwhe['is_delete'] = '0';

        $model = new CouponModel;
        $list = $model->getyouhuiList($couwhe,$this->user);
        if(count($list)>0){
            foreach ($list as $k=>$val){
                $goods= '';
                if($val['apply_range'] == '20'&&!empty($val['apply_good'])){
                    $where['apply_good'] = $val['apply_good'];
                    $goods = $model->couponGoodsDan($where['apply_good']);
                    if(!empty($goods['good_image'])){
                        $goods['good_imagead'] = $this->imgadess.$goods['good_image'].'.jpg';
                    }
                    $list[$k]['goods'] =$goods;
                }
            }
        }

        return $this->renderSuccess('1000','成功',$list);
    }

    /**
     * 优惠券添加
     */
    public function getYouHuiAdd()
    {
        $where = $this->request->post();
        if(empty($where['apply_range'])||empty($where['total_num'])||empty($where['min_price'])||empty($where['reduce_price'])||empty($where['start_time'])||empty($where['end_time'])){
            return $this->renderError('1001','参数不能为空',$update=[]);
        }

        if(strtotime($where['start_time']) >=strtotime($where['end_time'])){
            return $this->renderError('1003','优惠券开始时间不能小于结束时间',$update=[]);
        }
        if(strtotime($where['end_time'])<time()){
            return $this->renderError('1004','优惠券结束时间不能小于当前时间',$update=[]);
        }

        $update['apply_range'] = $where['apply_range'];
        if($where['apply_range'] == '20'){
            if(empty($where['apply_good'])){
                return $this->renderError('1001','商品ID不能为空');
            }
            $update['apply_good'] = $where['apply_good'];
        }
        $update['total_num'] = $where['total_num'];
        $update['min_price'] = $where['min_price'];
        $update['reduce_price'] = $where['reduce_price'];
        $update['start_time'] = strtotime($where['start_time']);
        $update['end_time'] = strtotime($where['end_time']);
        $update['merchant_id'] = $this->user['merchant_user_id'];

        $model = new CouponModel;
        if($model->getyouhuiadd($update)){
            return $this->renderSuccess('1000','成功');
        };
        return $this->renderError('1002','失败',$update=[]);
    }


    /**
     * 申请代理
     */
    public function daiLiAdd()
    {
        $where = $this->request->post();
        if(empty($where['daili_name'])||empty($where['province_id'])||empty($where['city_id'])||empty($where['district_id'])||empty($where['jine'])||empty($where['setup_daili_id'])){
            return $this->renderError('1001','参数不能为空',$update=[]);
        }
        $update['daili_name'] = $where['daili_name'];
        $update['province_id'] = $where['province_id'];
        $update['city_id'] = $where['city_id'];
        $update['district_id'] = $where['district_id'];
        $update['setup_daili_id'] = $where['setup_daili_id'];
        $update['jine'] = $where['jine'];
        $update['merchant_id'] = $this->user['merchant_id'];

        $model = new CouponModel;
        if($model->dailiadd($update)){
            return $this->renderSuccess('1000','成功');
        };
        return $this->renderError('1002','失败',$update=[]);
    }


    public function couponGoods()
    {
        $info = $this->request->post();
        $where['state'] = $info['tates'];
        $where['user_id'] = $this->user['merchant_user_id'];
        $where['is_on_sale'] = 2;
        $model = new CouponModel;
        $list = $model->couponGoods($where);
        return $this->renderSuccess('1000','成功',$list);
    }

    /*
     * 优惠券删除
     * */
    public function couponDel()
    {
        $info = $this->request->post();
        if(empty($info['coupon_id'])){
            return $this->renderError('1001','参数不能为空');
        }
        $where['coupon_id'] = $info['coupon_id'];
        $info['is_delete'] = 1;
        $model = new CouponModel;
        $list = $model->couponDel($where,$info);
        if($list){
            return $this->renderSuccess('1000','成功');
        }
        return $this->renderSuccess('1002','失败');
    }

    /*
     * 代理类型
     * */
    public function dailiList()
    {
        $info = $this->request->post();
        $model = new CouponModel;
        $list = $model->dailiList();

        return $this->renderSuccess('1000','成功',$list);

    }

    /*
         * liaotian
         * */
    public function chatAdd()
    {
        $info = $this->request->post();
        if(empty($info['userid'])||empty($info['is_img'])||empty($info['merchant_chat_name']))
        {
            return $this->renderError('1001','缺少必须参数');
        }
        $where['merchantid1'] = $this->user['merchant_id'];
        $where['merchantid2'] = $info['userid'];
        $where['merchant_chat_name'] = $info['merchant_chat_name'];
        $where['is_img'] = $info['is_img'];
        $where['create_time'] = time();
        $where['state2'] = '2';
        $model = new CouponModel;
        $list = $model->chatAdd($where);
        if($list){
            return $this->renderSuccess('1000','成功');
        }
        return $this->renderSuccess('1002','失败');
    }

    /*
         * liaotian
         * */
    public function chatList()
    {
        $info = $this->request->post();
        if(empty($info['userid']))
        {
            return $this->renderError('1001','缺少必须参数');
        }
        $where['a.merchantid1'] = $this->user['merchant_id'];
        $where['a.merchantid2'] = $info['userid'];
        $model = new CouponModel;
        $list = $model->chatList($where);
        $list['data'] = array_reverse($list['data']);
        if(count($list['data'])>0){
            $cateid = [];
            foreach ($list['data'] as $k=>$v){
                if($v['is_img'] ==2){
                    $list['data'][$k]['merchant_chat_name'] = $this->imgadess.$v['merchant_chat_name'].'.jpg';
                    $list['data'][$k]['create_time'] = date('Y-m-d H:i',$v['create_time']);
                }
                $list['data'][$k]['logo'] = $this->imgadess.$v['logo'].'.jpg';
                $list['data'][$k]['avatarUrl'] = $this->imgadess.$v['avatarUrl'].'.jpg';
                if($v['state1'] ==1){
                    $cateid[] = $v['merchant_chat_id'];
                }
            }

            if(count($cateid)>0){
                $model->tuixinind($cateid);
            }
        }

        return $this->renderSuccess('1000','成功',$list);

    }
    /*
     * liaotian
     * */
    public function tuixinList()
    {
        $info = $this->request->post();
        if(empty($info['type']))
        {
            return $this->renderError('1001','缺少必须参数');
        }
        if($info['type'] =='7')
        {
            $where['merchant_pid'] = $this->user['merchant_id'];
            $model = new CouponModel;
            $list = $model->tuiuserList($where);
            if(count($list)>0){
                foreach ($list as $k=>$v){
                    if(!empty($v['avatarUrl'])){
                        $list[$k]['avatarUrlad'] = $this->imgadess.$v['avatarUrl'].'.jpg';
                        $list[$k]['create_time'] = date('Y-m-d H:i',$v['create_time']);
                    }else{
                        $list[$k]['avatarUrlad'] = '';
                    }
                }
            }
            return $this->renderSuccess('1000','成功',$list);
        }
        $where['state'] = $info['type'];
        $where['merchant_pid'] = $this->user['merchant_id'];
        $where['is_delete'] = ['2,5'];
        $model = new CouponModel;
        $list = $model->tuixinList($where);
        if(count($list)>0){
            foreach ($list as $k=>$v){
                if(!empty($v['logo'])){
                    $list[$k]['logoad'] = $this->imgadess.$v['logo'].'.jpg';
                    $list[$k]['create_time'] = date('Y-m-d H:i',$v['create_time']);
                }else{
                    $list[$k]['logoad'] = '';
                }
            }
        }
        return $this->renderSuccess('1000','成功',$list);
    }

    /*
        * liaotian
        * */
    public function xioaxizui()
    {
        $info = $this->request->post();
        $where['merchantid1'] = $this->user['merchant_id'];
        $where['state1'] = 1;
        $model = new CouponModel;
        $list = $model->xioaxizui($where);
        if(count($list)>0){
            foreach ($list as $k=>$v){
                if($v['is_img'] ==2){
                    $list[$k]['merchant_chat_name'] = $this->imgadess.$v['merchant_chat_name'].'.jpg';
                    $list[$k]['create_time'] = date('Y-m-d H:i',$v['create_time']);
                }
                $list[$k]['avatarUrl'] = $this->imgadess.$v['avatarUrl'].'.jpg';
            }
        }
        return $this->renderSuccess('1000','成功',$list);
    }

}